package jym.tan.tmdb.method;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import jym.tan.tmdb.TMDB;
import jym.tan.tmdb.constant.TMDBConstants;
import jym.tan.tmdb.json.Parser;

import org.apache.commons.lang.StringUtils;

/**
 * The Class BaseMethod.
 *
 * @author Tanmoy
 */
public abstract class Base {

	protected TMDB tmdb;
	private String procedure;
	private String subprocedure;
	
	protected final Parser parser = Parser.getInstance();
	
	/**
	 * Constructor.
	 *
	 * @param tmdb 
	 * 		An instance of the {@link TMDB} class
	 * @param procedure 
	 * 		The procedure
	 */
	public Base(TMDB tmdb, String procedure) {
		this(tmdb, procedure, TMDBConstants.BLANK);
	}

	/**
	 * Constructor.
	 *
	 * @param tmdb 
	 * 		An instance of the {@link TMDB} class
	 * @param procedure 
	 * 		The procedure
	 * @param subprocedure 
	 * 		The subprocedure
	 */
	public Base(TMDB tmdb, String procedure, String subprocedure) {
		this.tmdb = tmdb;
		this.procedure = procedure;
		this.subprocedure = subprocedure;
	}
	
	/**
	 * Gets the url.
	 *
	 * @param query 
	 * 		The query to be searched
	 * @param id 
	 * 		The TMDB id / the IMDB id / Person id
	 * @param language 
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank
	 * 		The expected value is a ISO 639-1 code.
	 * @param country 
	 * 		The country parameter will filter the results to only include titles in that 
	 * 		particular country. Remember, if a translation exists, it might not have an 
	 * 		entry here. The expected value is a ISO 3166-1 code
	 * @param page 
	 * 		Some searches will have more than 20 results, the default 
	 * 		number of items returned per page. To iterate through use the page parameter
	 * @param includeAdult 
	 * 		You can toggle whether or not to include adult items in 
	 * 		your search by using this parameter. The expected value is either true or false
	 * @param requestToken
	 * 		The request token
	 * @return 
	 * 		The search url
	 */
	protected URL getUrl(String query, String id, String language, String country, int page, boolean includeAdult, String requestToken) {
        StringBuilder urlString = new StringBuilder(TMDBConstants.BASE_URL);
        urlString.append(procedure);

        if (StringUtils.isNotBlank(query)) {
            urlString.append(TMDBConstants.QUESTION_MARK);
            urlString.append(TMDBConstants.PARAM_QUERY);

            try {
                urlString.append(URLEncoder.encode(query, TMDBConstants.UTF8));
            } catch (UnsupportedEncodingException ex) {
                urlString.append(query);
            }
        }

        if (StringUtils.isNotBlank(id)) {
            urlString.append(id);
        }

        urlString.append(subprocedure);

        if (StringUtils.isBlank(query)) {
            urlString.append(TMDBConstants.QUESTION_MARK);
        } else {
            urlString.append(TMDBConstants.AMPERSAND);
        }
        urlString.append(TMDBConstants.PARAM_API_KEY);
        urlString.append(tmdb.getKey());

        if (StringUtils.isNotBlank(language)) {
            urlString.append(TMDBConstants.PARAM_LANGUAGE);
            urlString.append(language);
        }

        if (StringUtils.isNotBlank(country)) {
            urlString.append(TMDBConstants.PARAM_COUNTRY);
            urlString.append(country);
        }
        
        if (page > TMDBConstants.ZERO) {
            urlString.append(TMDBConstants.PARAM_PAGE);
            urlString.append(page);
        }
        
        if(includeAdult) {
        	urlString.append(TMDBConstants.PARAM_INCLUDE_ADULT);
        	urlString.append(includeAdult);
        }
        
        if(StringUtils.isNotBlank(requestToken)) {
        	urlString.append(TMDBConstants.PARAM_REQUEST_TOKEN);
        	urlString.append(requestToken);
        }
        
        try {
            return new URL(urlString.toString());
        } catch (MalformedURLException ex) {
            return null;
        }
    }	
	
	/**
	 * Gets the url.
	 *
	 * @param query 
	 * 		The query to be searched
	 * @param id 
	 * 		The TMDB id / the IMDB id / Person id
	 * @param language 
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank
	 * 		The expected value is a ISO 639-1 code.
	 * @param country 
	 * 		The country parameter will filter the results to only include titles in that 
	 * 		particular country. Remember, if a translation exists, it might not have an 
	 * 		entry here. The expected value is a ISO 3166-1 code
	 * @param page 
	 * 		Some searches will have more than 20 results, the default 
	 * 		number of items returned per page. To iterate through use the page parameter
	 * @param includeAdult 
	 * 		You can toggle whether or not to include adult items in 
	 * 		your search by using this parameter. The expected value is either true or false
	 * @return 
	 * 		The search url
	 */
	protected URL getUrl(String query, String id, String language, String country, int page, boolean includeAdult) {
		return getUrl(query, id, language, country, page, includeAdult, TMDBConstants.BLANK);
	}
	
	/**
	 * Gets the url.
	 *
	 * @param id 
	 * 		The TMDB id / the IMDB id / Person id
	 * @param language 
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank. 
	 * 		The expected value is a ISO 639-1 code
	 * @param country 
	 * 		The country parameter will filter the results to only include titles in that 
	 * 		particular country. Remember, if a translation exists, it might not have an 
	 * 		entry here. The expected value is a ISO 3166-1 code
	 * @return 
	 * 		The search url
	 */
	protected URL getUrl(String id, String language, String country) {
		return getUrl(TMDBConstants.BLANK, id, language, country, TMDBConstants.ZERO, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the url.
	 *
	 * @param id 
	 * 		The TMDB id / the IMDB id / Person id
	 * @param language 
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank. 
	 * 		The expected value is a ISO 639-1 code
	 * @return 
	 * 		The search url
	 */
	
	protected URL getUrl(String id, String language) {
		return getUrl(TMDBConstants.BLANK, id, language, TMDBConstants.BLANK, TMDBConstants.ZERO, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the url.
	 *
	 * @param id 
	 * 		The TMDB id / the IMDB id / Person id
	 * @return 
	 * 		The search url
	 */
	protected URL getUrl(String id) {
		return getUrl(TMDBConstants.BLANK, id, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.ZERO, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the url.
	 *
	 * @param query
	 * 		The query to be searched
	 * @param page
	 * 		Some searches will have more than 20 results, the default 
	 * 		number of items returned per page. To iterate through use the page parameter
	 * @return
	 * 		The search url
	 */
	protected URL getUrl(String query, int page) {
		return getUrl(query, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, page, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the url.
	 *
	 * @return
	 * 		The search url
	 */
	protected URL getUrl() {
		return getUrl(TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.ZERO, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the auth url.
	 *
	 * @return
	 * 		The search url
	 */
	protected URL getAuthUrl() {
		return getUrl(TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.ZERO, TMDBConstants.FALSE);
	}
	
	/**
	 * Gets the auth url.
	 *
	 * @param requestToken
	 * 		The request token
	 * @return
	 * 		The search url
	 */
	protected URL getAuthUrl(String requestToken) {
		return getUrl(TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.BLANK, TMDBConstants.ZERO, TMDBConstants.FALSE, requestToken);
	}
	
	/**
	 * Gets the Json object.
	 *
	 * @param url 
	 * 		The url
	 * @param clazz 
	 * 		The class of the object to be converted into
	 * @return 
	 * 		The Json object
	 * @throws Exception 
	 * 		If any kind off exception occurred
	 */
	protected <T> T getJsonObject(URL url, Class<T> clazz) throws Exception {
		String json = tmdb.getBrowser().request(url);
		return (T) parser.parseJson(json, clazz);
	}
}
